package com.winskysoft.frame;

import java.io.IOException;

import org.eclipse.jdt.internal.core.JavaProject;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

import com.winskysoft.entity.Table;
import com.winskysoft.tool.DatabaseTool;
import com.winskysoft.velocity.VelocityTool;
import org.eclipse.swt.events.SelectionAdapter;

public class CheckPanel extends Composite implements SelectionListener {
	private Text text;
	private Label lblNewLabel;
	private JavaProject project;
	private Button btnEButton;
	private Button btnMButton;
	private Button btnSButton;
	private Button btnCButton;

	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */
	public CheckPanel(Composite parent, int style) {
		super(parent, style);

		project = (JavaProject) getParent().getData();
		btnEButton = new Button(this, SWT.CHECK);
		btnEButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
			}
		});

		btnEButton.setBounds(30, 60, 93, 16);
		btnEButton.setText("Entity");
		btnEButton.setSelection(true);

		btnMButton = new Button(this, SWT.CHECK);
		btnMButton.setBounds(30, 100, 93, 16);
		btnMButton.setText("Mapper");
		btnMButton.setSelection(true);

		btnSButton = new Button(this, SWT.CHECK);
		btnSButton.setBounds(30, 140, 93, 16);
		btnSButton.setText("Service");
		btnSButton.setSelection(true);

		btnCButton = new Button(this, SWT.CHECK);
		btnCButton.setBounds(30, 180, 93, 16);
		btnCButton.setText("Controller");
		btnCButton.setSelection(true);

		Button btnNewButton = new Button(this, SWT.NONE);
		btnNewButton.addSelectionListener(this);
		btnNewButton.setLocation(437, 284);
		btnNewButton.setSize(79, 34);
		btnNewButton.setText("\u5B8C\u6210");

		lblNewLabel = new Label(this, SWT.NONE);
		lblNewLabel.setText("生成对象选择");
		lblNewLabel.setFont(SWTResourceManager.getFont("宋体", 12, SWT.NORMAL));
		lblNewLabel.setBounds(27, 10, 171, 24);

		// lblNewLabel.setText(project.getProject().getName());

	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

	@Override
	public void widgetSelected(SelectionEvent e) {
		String path = project.getProject().getLocation().toString();
		boolean be = btnEButton.getSelection();
		boolean bm = btnMButton.getSelection();
		boolean bs = btnSButton.getSelection();
		boolean bc = btnCButton.getSelection();

		Table[] table = DatabaseTool.getSelectList();

		String st = "";

		try {
			for (Table strs : table) {
				String str = project.getProject().getName();
				if (be) {
					VelocityTool
							.writeTemp(project.getProject().getName(),
									"/template/entity.vm", "UTF-8", strs,
									path + "/src/main/java/normal/com/lxsh/" + str
											+ "/entity/", "");
				}
				if (bc) {
					VelocityTool
							.writeTemp(project.getProject().getName(),
									"/template/controller.vm", "UTF-8", strs,
									path + "/src/main/java/normal/com/lxsh/" + str
											+ "/controller/", "Controller");
				}
				if (bm) {
					VelocityTool.writeTemp(project.getProject().getName(),
							"/template/mapper.vm", "UTF-8", strs, path
									+ "/src/main/java/normal/com/lxsh/" + str + "/mapper/",
							"Mapper");
				}
				if (bs) {
					VelocityTool.writeTemp(project.getProject().getName(),
							"/template/service.vm", "UTF-8", strs, path
									+ "/src/main/java/normal/com/lxsh/" + str + "/service/",
							"Service");
					VelocityTool.writeTemp(project.getProject().getName(),
							"/template/serviceImpl.vm", "UTF-8", strs, path
									+ "/src/main/java/normal/com/lxsh/" + str
									+ "/service/impl/", "ServiceImpl");
				}
			}

		} catch (IOException e1) {
			e1.printStackTrace();
		}
		getParent().dispose();
	}

	@Override
	public void widgetDefaultSelected(SelectionEvent e) {
		// TODO Auto-generated method stub

	}

}
